package com.erp4j.dynamicfield.controller;

import com.erp4j.dynamicfield.entity.FieldDefinition;
import com.erp4j.dynamicfield.service.FieldDefinitionService;
import com.erp4j.dynamicfield.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/fields")
public class FieldController {

    @Resource
    private FieldDefinitionService fieldService;

    /**
     * 获取模块的所有字段（按排序号排序）
     */
    @GetMapping("/module/{moduleId}")
    public Result<List<FieldDefinition>> getFieldsByModuleId(@PathVariable Long moduleId) {
        return Result.success(fieldService.getFieldsByModuleId(moduleId));
    }

    /**
     * 添加字段
     */
    @PostMapping
    public Result<FieldDefinition> addField(@RequestBody FieldDefinition field) {
        return Result.success(fieldService.addField(field));
    }

    /**
     * 更新字段
     */
    @PutMapping
    public Result<FieldDefinition> updateField(@RequestBody FieldDefinition field) {
        fieldService.updateById(field);
        return Result.success(field);
    }

    /**
     * 删除字段
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteField(@PathVariable Long id) {
        fieldService.removeById(id);
        return Result.success();
    }
}
